﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace HelperFromTheSkeleton.ExtendMethod
{
    public static class ObjectExtend
    {
        /// <summary>
        /// 将两个对象中相同名称的字段进行复制赋值
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="target"></param>
        public static void CopyProperty(this object obj, object target)
        {
            var objProperties = obj.GetType().GetProperties();
            var targetProperties = target.GetType().GetProperties();
            foreach (var item in targetProperties)
            {
                var objPropertie = objProperties.FirstOrDefault(m => m.Name == item.Name);
                if(objPropertie == null) continue;
                var value = objPropertie.GetValue(obj);
                item.SetValue(target, value);
            }
        }
    }
}
